package lh.companion.track.server.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.companion.track.server.controller.support.AjaxResponse;
import lh.companion.track.server.model.RealTrack;
import lh.companion.track.server.netty.NettyServer;
import lh.companion.track.server.repository.RealTrackRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by lh on 2017/8/11.
 */
@RestController
@RequestMapping("/track")
public class RealTrackController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RealTrackRepository trackRepository;

    @Autowired
    private NettyServer nettyServer;

    @ApiOperation(value = "查询某个用户在某个活动的轨迹")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "activityId", value = "活动ID", required = true, paramType = "path", dataType = "long"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path", dataType = "long")
    })
    @RequestMapping(value = "/query/{activityId}/{userId}", method = RequestMethod.GET)
    public AjaxResponse query(@PathVariable Long activityId, @PathVariable Long userId) {
        List<RealTrack> trackList = trackRepository.findByActivityIdAndUserId(activityId, userId);
        return AjaxResponse.ok().data(trackList);
    }

    @ApiOperation(value = "添加活动轨迹空间")
    @RequestMapping(value = "/add/{activityId}", method = RequestMethod.POST)
    public AjaxResponse add(@PathVariable Long activityId) {
        nettyServer.createNamespace(activityId);
        return AjaxResponse.ok();
    }

    @ApiOperation(value = "删除活动轨迹空间")
    @RequestMapping(value = "/delete/{activityId}", method = RequestMethod.POST)
    public AjaxResponse delete(@PathVariable Long activityId) {
        nettyServer.deleteNamespace(activityId);
        return AjaxResponse.ok();
    }
}
